package com.muyu.danmo.mq;

import cn.hutool.json.JSONUtil;
import com.muyu.danmo.service.IDanmoOrderService;
import com.muyu.system.danmo.dto.PaymentCallBackResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import static com.muyu.common.core.constant.TagConstant.PAY_NOTICE_TAG;
import static com.muyu.common.core.constant.TopicConstant.COMMON_TOPIC_NORMAL;
import static com.muyu.system.danmo.util.ConsumerGroupConstant.ORDER_PAY_GROUP;

/**
 * @author Administrator
 */
@Component
@Slf4j
@RequiredArgsConstructor
@RocketMQMessageListener(
        topic = COMMON_TOPIC_NORMAL,
        consumerGroup = ORDER_PAY_GROUP,
        selectorExpression = PAY_NOTICE_TAG
)
public class PayCallbackMessageConsumer implements RocketMQListener<String> {

    private final IDanmoOrderService danmoOrderService;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void onMessage(String message) {
        log.info("订单模块接收到的支付结果:{}", message);
        PaymentCallBackResult bean = JSONUtil.toBean(message, PaymentCallBackResult.class);
        danmoOrderService.update(bean);

    }
}
